package com.OnTimeTeam.OnTime;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class NotifyAtBootReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
    	
    	//If device just started
        if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        	
            //In case there is any alarm on right
    		String path = context.getFilesDir().getPath().toString() + "/reminders.xml";
    		ArrayList<AlarmFeatures> alarmsArray = RemindersIO.getAllAlarms(path);
    		
    		for(int i = 0; i < alarmsArray.size(); ++i) {
    			if(alarmsArray.get(i).getStatus().equals("On")) {
    				
    				//Cancel Snooze
    				RemindersIO.changeSnoozeSign(alarmsArray.get(i).getID(), path, "None");
    				
    				//Handle Regular, Smart and Quick alarms Separately
    				if(alarmsArray.get(i).getSortOfAlarm().equals("Regular"))
    					AlarmSetter.setBackAlarm(alarmsArray.get(i), context, context);
    				if(alarmsArray.get(i).getSortOfAlarm().equals("Smart"))
    					AlarmSetter.setSmartAlarm((SmartAlarmFeatures)alarmsArray.get(i), context);
    				else if(alarmsArray.get(i).getSortOfAlarm().equals("Quick"))
    					RemindersIO.changeAlarmStatus(path, Integer.toString(i), "Off");
    					
    			}
    		}
    		
    		//Call notifications service
            Intent notifyService = new Intent("com.OnTimeTeam.OnTime.NotificationService");
            context.startService(notifyService);
        }
    }
    
    public static void logAllAlarms(ArrayList<AlarmFeatures> alarmsArray) {
    	
		for(int i = 0; i < alarmsArray.size(); ++i) {
			
			AlarmFeatures currentAF = alarmsArray.get(i);
			
			Log.e("Alarm","");
			Log.e("Sort", currentAF.getSortOfAlarm());
			Log.e("Status", currentAF.getStatus());
			Log.e("ID", currentAF.getID());
			Log.e("Label", currentAF.getLabel());
			Log.e("Days", currentAF.getDaysToRepeat());
			Log.e("IDS", currentAF.getDaysIDS());
			Log.e("Hour", currentAF.getHour());
			Log.e("Min", currentAF.getMinute());
		}
    }
}
